package com.hb.trans.mvp.presenter

import com.basiclib.base.BasePresenter
import com.basiclib.lifecycle.RxLifecycleUtils
import com.basiclib.network.RetryWithDelay
import com.basiclib.network.RxErrorHandler
import com.hb.trans.bean.Base
import com.hb.trans.bean.EnrollCount
import com.hb.trans.bean.SchoolDetail
import com.hb.trans.global.ErrorHandleSubscriber
import com.hb.trans.mvp.contract.SchoolInfoContract
import com.hb.trans.mvp.model.SchoolInfoModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class SchoolInfoPresenter @Inject constructor(model: SchoolInfoModel, var errorHandler: RxErrorHandler) :
    BasePresenter<SchoolInfoContract.View, SchoolInfoContract.Model>(model) {
    fun getSchoolCoach(id: String?) {
        if (!id.isNullOrBlank()) {
            mModel.getSchoolDetail(id)
                .subscribeOn(Schedulers.io())
                .retryWhen(RetryWithDelay())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
                .subscribe(object : ErrorHandleSubscriber<Base<SchoolDetail>>(errorHandler) {
                    override fun next(r: Base<SchoolDetail>) {
                        if (r.code == 200) {
                            mRootView?.setupSchoolDetail(r.data)
                        } else {
                            mRootView?.showShortToast(r.info ?: "")
                        }
                    }
                })
        }
    }

    fun getEnrollCount(inscode: String?) {
        if (!inscode.isNullOrBlank()) {
            mModel.getEnrollCount(inscode)
                .subscribeOn(Schedulers.io())
                .retryWhen(RetryWithDelay())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
                .subscribe(object : ErrorHandleSubscriber<Base<EnrollCount>>(errorHandler) {
                    override fun next(r: Base<EnrollCount>) {
                        if (r.code == 1) {
                            mRootView?.setupEnrollCount(r.data)
                        } else {
                            mRootView?.showShortToast(r.message ?: "")
                        }
                    }
                })
        }
    }
}